$(function () {
    var transferRecord_datagrid, transferRecord_search,transferRecord_removeAll,transferRecord_remove

    transferRecord_datagrid = $("#transferRecord_datagrid");
    transferRecord_search = $("#searchbtn");
    transferRecord_removeAll = $("#transferRecord_removeAll");
    transferRecord_remove = $("#transferRecord_remove");


    transferRecord_datagrid.datagrid({

        url: '/transferRecord/list',
        fit: true,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar:'#datagrid_toolar',
        columns: [[
            {field: 'realName', title: '被移交的客户', width: 100},
            {field: 'status', title: '客户状态', width: 100, formatter: stateFormatter},
            {field: 'currentName', title: '移交操作者', width: 100},
            {field: 'managerEmployee', title: '移交后的负责人', width: 100, formatter: employeeManagerFormatter},
            {field: 'beforeManagerEmployee', title: '移交前的负责人', width: 100, formatter: employeeBeforManagerFormatter},
            {field: 'reason', title: '移交原因', width: 100},
            {field: 'transferTime', title: '移交时间', width: 100},


        ]]

    });

    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })


    var cmdObj = {
        //清空
        transferRecord_removeAll:function(){
                $.messager.confirm("温馨提示","您确定要清除全部记录吗?",function(yes){
                    if(yes){
                        $.get("/transferRecord/removeAll",function(data){
                            if(data.success){
                                transferRecord_datagrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json")
                    }
                });

        },
        //删除
        transferRecord_remove:function(){
            var rowData = transferRecord_datagrid.datagrid("getSelected");
            if(rowData){
                $.messager.confirm("温馨提示","您确定要将该客户转正吗?",function(yes){
                    if(yes){
                        $.get("/transferRecord/remove?id="+rowData.id,function(data){
                            if(data.success){
                                transferRecord_datagrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json")
                    }
                });
            }else{
                $.messager.alert("温馨提示","请选择需要转正的客户.","warning");
            }
        },

        //刷新
        transferRecord_reload: function () {
            transferRecord_datagrid.datagrid("reload");
        }

    };

    //搜索
    transferRecord_search.textbox({
        buttonText: 'SEARCH',
        prompt: '搜索关键字',
        label: '关键字',
        labelPosition: 'left',
        labelWidth: 50,
        width: 300,
        onClickButton: function () {
            transferRecord_datagrid.datagrid("load", {
                keyword: transferRecord_search.val()
            });
        }
    });


});

function employeeManagerFormatter(value, record, index) {
    if (value) {
        return value.name;
    } else {
        return "";
    }
}

function employeeBeforManagerFormatter(value, record, index) {
    if (value) {
        return value.name;
    } else {
        return "";
    }
}

function stateFormatter(value, record, index) {
    if (value == 0) {
        return "<font color='#00CD66'>潜在客户</font>";
    } else if (value == 1) {
        return "<font color='red'>正式客户</font>";
    }
}
